package opl.ui;

import java.util.List;

import opl.entities.Team;
import opl.ui.adapters.TeamsAdapter;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TeamsActivity extends Activity implements OnItemClickListener {
	
	private List<Team> _lstTeams;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.teams);
		LoadTeams();
	}

	private void LoadTeams() {
		ListView lvTeams=(ListView) findViewById(R.id.lvTeams);
		_lstTeams=Team.getTeams(this);
		TeamsAdapter adptr=new TeamsAdapter(this, _lstTeams);
		lvTeams.setAdapter(adptr);
		lvTeams.setOnItemClickListener(this);
	}
	
	public void imgTeamLogo_Click(View v){
		LinearLayout ll = (LinearLayout) v.getParent();
		ll=(LinearLayout)ll.getChildAt(1);
		TextView tvName = (TextView) ll.getChildAt(0);
		loadTeamDetails(tvName.getText().toString());
	}

	private void loadTeamDetails(String teamName){
		Intent intTeam = new Intent(Intent.ACTION_VIEW,
				Uri.parse("opl://ui.team"));
		intTeam.putExtra("TeamName", teamName);
		startActivity(intTeam);
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Team team=_lstTeams.get(arg2);
		loadTeamDetails(team.getName());
	}
}
